<?php

namespace app\api\controller;

use app\common\controller\Api;
use fast\Random;
use think\Db;

/**
 * Token接口
 */
class Token extends Api
{
    protected $noNeedLogin = ['getToken'];
    protected $noNeedRight = '*';

    /**
     * 检测Token是否过期
     *
     */
    public function check()
    {
        $token = $this->auth->getToken();
        $tokenInfo = \app\common\library\Token::get($token);
        $this->success('', ['token' => $tokenInfo['token'], 'expires_in' => $tokenInfo['expires_in']]);
    }

    /**
     * 刷新Token
     *
     */
    public function refresh()
    {
        //删除源Token
        $token = $this->auth->getToken();
        \app\common\library\Token::delete($token);
        //创建新Token
        $token = Random::uuid();
        \app\common\library\Token::set($token, $this->auth->id, 2592000);
        $tokenInfo = \app\common\library\Token::get($token);
        $this->success('', ['token' => $tokenInfo['token'], 'expires_in' => $tokenInfo['expires_in']]);
    }


    public function getToken()
    {
        // $YopService = new \services\YopService();
        // $res = $YopService->wechatConfigQueryV2Test();
        // var_dump($res);exit;
        // $play_order = Db::name('play_order')->field('id,shop_no')->where('service_admin_id',0)->select();
        // foreach ($play_order as $key => $value) {
        //     $salesman_admin_id = Db::name('shop')->where('shop_no',$value['shop_no'])->value('salesman_admin_id');
        //     if ($salesman_admin_id) {
        //         $service_admin_by = Db::name('admin')->where('id',$salesman_admin_id)->value('nickname');
        //         Db::name('play_order')->where('id',$value['id'])->update(['service_admin_by'=>$service_admin_by,'service_admin_id'=>$salesman_admin_id]);
        //     }
        // }exit;
        $userid = $_GET['userid'];
        $token = Api::getRefreshtoken($userid); 
        var_dump($token);exit;
    }
}
